<?php 

class Option_model extends CI_Model{
	public function __construct() {
        parent::__construct();
		$this->load->library('upload');
    }
	
	function create($data){
		//$config['full_path'] = $this->input->post('image');
		$config = $this->upload->data('image');
		//$config['full_path'] = $this->input->post('image');
		$data = array(
					'option_name' => $this->input->post('option_name'),
					'price' =>  $this->input->post('price'),
					//'image' => $this->upload->data(),
					'image' => ''.$config['file_name'],
					'quantity' => $this->input->post('quantity'),
				);
		$this->db->insert('product_option',$data);
	}
	
	function getAll(){
		$query = $this->db->query('select * from product_option');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_option_by_id($product_option_id){
		$this->db->where('product_option_id', $product_option_id);
		$this->db->from('product_option');
		$query = $this->db->get();
		return $query->row_array();		
		}
	
	function update($product_option_id){
		$config = $this->upload->data('image');
		if(!empty($config['file_name'])){
			$data = array(
					'option_name' => $this->input->post('option_name'),
					'price' =>  $this->input->post('price'),
					'image' =>  ''.$config['file_name'],
					'quantity' => $this->input->post('quantity'),
				);
        $this->db->where('product_option_id', $product_option_id);
        $this->db->update('product_option', $data);
		}else{
			$data = array(
						'option_name' => $this->input->post('option_name'),
						'price' =>  $this->input->post('price'),
						//'image' =>  ''.$config['file_name'],
						'quantity' => $this->input->post('quantity'),
					);
			$this->db->where('product_option_id', $product_option_id);
			$this->db->update('product_option', $data);
		}
		
    }
	
	function select($product_option_id){
        return $this->db->get_where('product_option', array('product_option_id'=>$product_option_id))->row();
    }
	
	function delete($product_option_id){
        $this->db->delete('product_option', array('product_option_id'=>$product_option_id));
    }
}
?>